package com.example.asynctaskdemo;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.LruCache;
import android.widget.ImageView;

public class ImageLoader {
	
	private ImageView mImageView;
	private String mUrl;
	private LruCache<String, Bitmap> mCache;
	
	public ImageLoader(){
		//获取最大可用内存
		int maxMemory = (int) Runtime.getRuntime().maxMemory();
		int cacheSize = maxMemory/4;
		mCache = new LruCache<String, Bitmap>(cacheSize){
			
			@Override
			protected int sizeOf(String key, Bitmap value) {
				//每次加入缓存的时候调用
				return value.getByteCount();
			}
		};
	}
	
	public void addBitmapToCache(String url, Bitmap bitmap){
		if (getBitmapFromCache(url) == null) {
			mCache.put(url, bitmap);
		}
	}
	public Bitmap getBitmapFromCache(String url){
		//底层是LinkedHashMap实现
		return mCache.get(url);
	}

	public void showImageByAsyncTask(ImageView imageView, final String url){
		Bitmap bitmap = getBitmapFromCache(url);
		if (bitmap == null) {
			new ImageAsyncTask(imageView,url).execute(url);
		}else {
			imageView.setImageBitmap(bitmap);
		}
	}
	
	class ImageAsyncTask extends AsyncTask<String, Void, Bitmap>{
		
		private ImageView mImageView;
		private String mUrl;
		
		public ImageAsyncTask(ImageView imageView, String url) {
			mImageView = imageView;
			mUrl = url;
		}
		@Override
		protected Bitmap doInBackground(String... params) {
			Bitmap bitmap = getBitmap(params[0]);
			//将网络获取到的图片保存到缓存中
			if (bitmap != null) {
				addBitmapToCache(params[0], bitmap);
			}
			return bitmap;
		}
		@Override
		protected void onPostExecute(Bitmap result) {
			super.onPostExecute(result);
			if (mImageView.getTag().equals(mUrl)) {
				mImageView.setImageBitmap(result);
			}
		}
	}
	
	private Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (mImageView.getTag().equals(mUrl)) {
				mImageView.setImageBitmap((Bitmap) msg.obj);
			}
		};
	};
	
	public void showImageByThread(ImageView imageView, final String url){
		mImageView = imageView;
		mUrl = url;
		new Thread(){
			@Override
			public void run() {
				super.run();
				Bitmap bitmap = getBitmap(url);
				Message message = new Message();
				message.obj = bitmap;
				handler.sendMessage(message);
			}
		}.start();
	}
	public Bitmap getBitmap(String urlString){
		Bitmap bitmap = null;
		InputStream is = null;
		try {
			URL url = new URL(urlString);
			try {
				HttpURLConnection connection = (HttpURLConnection) url.openConnection();
				is = new BufferedInputStream(connection.getInputStream());
				bitmap = BitmapFactory.decodeStream(is);
				connection.disconnect();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return bitmap;
	}
}
